package org.jeecg.modules.knowledgecomment.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.comment.entity.PhilComment;
import org.jeecg.modules.comment.service.IPhilCommentService;
import org.jeecg.modules.comment.vo.CommentPublish;
import org.jeecg.modules.knowledgecomment.entity.KnowledgeComment;
import org.jeecg.modules.knowledgecomment.service.IKnowledgeCommentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: knowledge_comment
 * @Author: jeecg-boot
 * @Date:   2020-12-30
 * @Version: V1.0
 */
@Api(tags="知识评论")
@RestController
@RequestMapping("/knowledgeComment")
@Slf4j
public class KnowledgeCommentController extends JeecgController<KnowledgeComment, IKnowledgeCommentService> {
	@Autowired
	private IKnowledgeCommentService knowledgeCommentService;
	@Autowired
	private IPhilCommentService philCommentService;
	/**
	 * 分页列表查询
	 *
	 * @param knowledgeComment
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "knowledge_comment-分页列表查询")
	@ApiOperation(value="knowledge_comment-分页列表查询", notes="knowledge_comment-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(PhilComment knowledgeComment,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<PhilComment> queryWrapper = QueryGenerator.initQueryWrapper(knowledgeComment, req.getParameterMap());
		// 控制查找什么类型的数据
		queryWrapper.eq("status", req.getParameter("status")==null?0:req.getParameter("status"));
		queryWrapper.eq("type",0);
		Page<PhilComment> page = new Page<PhilComment>(pageNo, pageSize);
		IPage<PhilComment> pageList = philCommentService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param knowledgeComment
	 * @return
	 */
	@AutoLog(value = "knowledge_comment-添加")
	@ApiOperation(value="knowledge_comment-添加", notes="knowledge_comment-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody KnowledgeComment knowledgeComment) {
		PhilComment comment = new PhilComment();
		BeanUtils.copyProperties(knowledgeComment,comment);
		comment.setType(0);
		comment.setStatus(0);
		comment.setArticleId(knowledgeComment.getKnowledgeId());
		philCommentService.save(comment);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param knowledgeComment
	 * @return
	 */
	@AutoLog(value = "knowledge_comment-编辑")
	@ApiOperation(value="knowledge_comment-编辑", notes="knowledge_comment-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody PhilComment knowledgeComment) {
		philCommentService.updateById(knowledgeComment);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "知识评论-通过id删除")
	@ApiOperation(value="知识评论-通过id删除", notes="知识评论-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		philCommentService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "知识评论-批量删除")
	@ApiOperation(value="知识评论-批量删除", notes="知识评论-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.philCommentService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "知识评论-通过id查询")
	@ApiOperation(value="知识评论-通过id查询", notes="知识评论-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		PhilComment knowledgeComment = philCommentService.getById(id);
		if(knowledgeComment==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(knowledgeComment);
	}

	 /**
	  * 通过知识id查询评论
	  *
	  * @param articleId
	  * @return
	  */
	 @AutoLog(value = "app-通过知识id查询评论")
	 @ApiOperation(value="app-通过知识id查询评论", notes="app-通过知识id查询评论")
	 @GetMapping(value = "/queryByKnowledgeId")
	 public Result<?> queryByArticleId(@RequestParam("id") String articleId,
									   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									   HttpServletRequest req) {
		 Page<CommentPublish> page = new Page<>(pageNo, pageSize);
		 IPage<CommentPublish> pageList = knowledgeCommentService.queryListPublishPage(page, articleId);
		 return Result.ok(pageList);
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param knowledgeComment
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, KnowledgeComment knowledgeComment) {
        return super.exportXls(request, knowledgeComment, KnowledgeComment.class, "knowledge_comment");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, KnowledgeComment.class);
    }

}
